$(function(){
	//点击刷新验证码
	$("#imgid").click(function(){
		//模板分配过来的变量
	    VerifyUrl=VerifyUrl.replace('.html','');
		$(this).attr('src',VerifyUrl+'/'+Math.random());
	});
	var num1,num2,num3,num4;
	//表单验证
	//验证手机号码
	$("input[name=tel]").blur(function(){
		var re=new RegExp(/^\d{11}$/);
		if(!($(this).val().match(re))){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).css('border','1px solid red');

			error_text('手机号码格式错误','red');
			num1=0;
		}else{
			getTel();
		}
	});

	//验证密码和确认密码
	$("input[name=pwd]").blur(function(){
		if(!$(this).val().match(/^[a-zA-Z][\w]{4,16}$/)){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).css('border','1px solid red');
			error_text('密码由字母开头，数字 _ 组成5-17位','red');
			num2=1;
		}else{
			$(this).next().attr('src',pub+'/Images/checked.gif').show();
			$(this).css('border','1px solid lightblue');
			error_text('','green');
			num2=2;
		}
	});

	$("input[name=pwded]").blur(function(){
		if($(this).val()!=$("input[name=pwd]").val()){
			$(this).next().attr('src',pub+'/Images/unchecked.gif').show();
			$(this).css('border','1px solid red');
			error_text('两次密码不一致','red');
			num3=1;
		}else{
			$(this).next().attr('src',pub+'/Images/checked.gif').show();
			$(this).css('border','1px solid lightblue');
			error_text('','green');
			num3=3;
		}
	});

	//验证码
	$("input[name=verify]").blur(function(){
		var verify = $(this).val();
		$.ajax({
			"type" : "post",
			"data" : {"verify":verify},
			"url" : checkVerify,
			"dataType" : "json",
			"success" : function(d){
				if(d=='ok'){
					$("input[name=verify]").next().next().attr('src',pub+'/Images/checked.gif').show();
					$("input[name=verify]").css('border','1px solid lightblue');
					error_text('','green');
					num1=1
				}else{
					$("input[name=verify]").next().next().attr('src',pub+'/Images/unchecked.gif').show();
					$("input[name=verify]").css('border','1px solid red');
					error_text('验证码错误','red');
					num1=0
				}
			}
			
		});
	});

	//异步验证手机号
	function getTel(){
		var tel = $("input[name=tel]").val();
		var str;
			$.ajax({
				"type" : "post",
				"data" : {"tel":tel},
				"url" : checkTel,
				"dataType" : "json",
				"success" : function(d){
					if(d=='ok'){
						$("input[name=tel]").next().attr('src',pub+'/Images/checked.gif').show();
						$("input[name=tel]").css('border','1px solid lightblue');
						error_text('','green');
						num4=4;
					}else{
						$("input[name=tel]").next().attr('src',pub+'/Images/unchecked.gif').show();
						$("input[name=tel]").css('border','1px solid red');
						error_text('该手机号已注册','red');
						num4=1;
					}
				}
			});
	}

	//验证提交按钮
	$("input[name=sub]").click(function(){
		if(($("input[name=tel]").val() == "") || ($("input[name=verify]").val() == "") || ($("input[name=pwd]").val() == "") || ($("input[name=pwded]").val() == "") || num1+num2+num3+num4!=10){
				return false;
			}
			
	});

	function error_text(data,rgb){
		$('#error').html(data).css('color',rgb).fadeIn();
	}
});